import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs, make_circles
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from model import SVM


def main():
    # 示例1：线性可分数据
    print("示例1：线性可分数据")
    X, y = make_blobs(n_samples=100, centers=2, random_state=42)
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.2, random_state=42)

    # 创建线性核SVM模型
    svm = SVM(kernel_type='linear', C=1.0, max_iter=1000)
    svm.fit(X_train, y_train)

    # 评估模型
    train_accuracy = svm.score(X_train, y_train)
    test_accuracy = svm.score(X_test, y_test)

    print(f"训练集准确率: {train_accuracy:.4f}")
    print(f"测试集准确率: {test_accuracy:.4f}")

    # 绘制决策边界
    svm.plot_decision_boundary(X, y, title="SVM with Linear Kernel")

    # 示例2：非线性数据
    print("\n示例2：非线性数据")
    X, y = make_circles(n_samples=100, noise=0.1, factor=0.5, random_state=42)
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.2, random_state=42)

    # 创建RBF核SVM模型
    svm_rbf = SVM(kernel_type='rbf', C=1.0, max_iter=1000)
    svm_rbf.fit(X_train, y_train)

    # 评估模型
    train_accuracy = svm_rbf.score(X_train, y_train)
    test_accuracy = svm_rbf.score(X_test, y_test)

    print(f"训练集准确率: {train_accuracy:.4f}")
    print(f"测试集准确率: {test_accuracy:.4f}")

    # 绘制决策边界
    svm_rbf.plot_decision_boundary(X, y, title="SVM with RBF Kernel")


if __name__ == "__main__":
    main()
